package indi.jp.project.core.service.impl;

import indi.jp.project.core.dao.BaseDao;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.entity.Menu;
import indi.jp.project.core.entity.Permission;
import indi.jp.project.core.entity.Role;
import indi.jp.project.core.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements PermissionService {

    private BaseDao baseDao;

    public PermissionServiceImpl(BaseDao baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public Map<String, Object> queryGrid(Permission entity) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        List<Role> list = baseDao.queryList("permissionMapper.queryList", entity);
        int count = baseDao.queryCount("permissionMapper.queryCount", entity);
        map.put("grid", list);
        map.put("total", count);
        return map;
    }

    @Override
    public JsonResult save(Permission entity) throws SQLException {
        if ("".equals(entity.getId())) {
            baseDao.insert("permissionMapper.save", entity);
        } else {
            baseDao.insert("permissionMapper.edit", entity);
        }
        return JsonResult.success("");
    }

    @Override
    public int deleteById(String id) throws SQLException {
        return baseDao.deleteById("permissionMapper.deleteById", id);
    }

    @Override
    public JsonResult queryPermissions() {
        try {
            List<Menu> list = baseDao.queryList("permissionMapper.queryPermissions", "");
            return JsonResult.success(list);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Transactional(rollbackFor = SQLException.class)
    @SuppressWarnings("unchecked")
    public void savePermissions(Map<String, Object> map) throws SQLException {
        String roleId = String.valueOf(map.get("roleId"));
        List<Map<String, String>> menu = (List<Map<String, String>>) map.get("menu");
        List<Map<String, String>> permission = (List<Map<String, String>>) map.get("operation");
        baseDao.delete("permissionMapper.deleteRoleMenu", roleId);
        baseDao.delete("permissionMapper.deleteRolePermission", roleId);
        if (menu.size() > 0) {
            baseDao.insert("permissionMapper.saveRoleMenu", menu);
        }
        if (permission.size() > 0) {
            baseDao.insert("permissionMapper.saveRolePermission", permission);
        }
    }

    @Override
    public JsonResult queryRolePermissions(String roleId) {
        try {
            List<String> list = new ArrayList<>();
            List<String> menuIds = baseDao.queryList("permissionMapper.queryRoleMenu", roleId);
            List<String> permissionIds = baseDao.queryList("permissionMapper.queryRolePermission", roleId);
            list.addAll(menuIds);
            list.addAll(permissionIds);
            return JsonResult.success(list);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }


}
